package com.woniuxy.moneyapp.app.consumer;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.commons.exception.MyException;
import com.woniuxy.commons.util.BeanCopyUtil;
import com.woniuxy.commons.util.DateUtil;
import com.woniuxy.commons.util.Result;
import com.woniuxy.moneyapp.app.controller.form.RechargeStateInfo;
import com.woniuxy.moneyapp.common.enums.PaymentRelated;
import com.woniuxy.moneyapp.common.enums.SchemaCode;
import com.woniuxy.moneyapp.common.exception.code.FundExceptionCode;
import com.woniuxy.moneyapp.infra.dto.RecordForm;
import com.woniuxy.moneyapp.infra.model.CashoutRecord;
import com.woniuxy.moneyapp.infra.model.ReturnCash;
import com.woniuxy.moneyapp.service.CashoutRecordService;
import com.woniuxy.moneyapp.service.PayRecordService;
import com.woniuxy.moneyapp.service.ReturnCashService;
import com.woniuxy.ptapp.client.ClassesOrderClient;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;


@Component
@GlobalTransactional(rollbackFor = Exception.class)
@RocketMQMessageListener(consumerGroup = "fund_group_add_info",topic = "msgsend",
        selectorExpression = "add_info")
public class AddInfoComsumer implements RocketMQListener<RechargeStateInfo> {

    @Resource
    private PayRecordService payRecordService;

    @Resource
    private CashoutRecordService cashoutRecordService;

    @Resource
    private ClassesOrderClient coc;

    @Resource
    private ReturnCashService rc;

    @Resource
    private StringRedisTemplate srt;

    @Override
    public void onMessage(RechargeStateInfo orderInfo) {

            //根据 scence 判断往那张表添加数据
            String s = srt.opsForValue().get("repeat:defend:info:"+orderInfo.getOut_trade_no());
            //防止重消
            if(s!=null){
                return;
            }
            RecordForm record = BeanCopyUtil.copyObject(orderInfo, RecordForm.class);
            System.out.println(record);
            if(orderInfo.getScence()==2){
                //提现
                CashoutRecord cashoutRecord = new CashoutRecord();
                cashoutRecord.setUserId(orderInfo.getPayeeId());
                cashoutRecord.setCashAmount(orderInfo.getCashAmount());
                cashoutRecord.setState(1);
                cashoutRecord.setPayment(PaymentRelated.ALIPAY_NAME.getDesc());
                cashoutRecord.setScence(SchemaCode.CASHOUT_CODE.getNum());
                cashoutRecord.setCashoutNum(orderInfo.getOut_trade_no());
                if(!cashoutRecordService.save(cashoutRecord)) throw new MyException(FundExceptionCode.DAO_ERROR.getCode(),FundExceptionCode.DAO_ERROR.getMsg());
                //cashoutRecordService.getOne(new QueryWrapper<CashoutRecord>().eq(""))
                record.setScenceId(cashoutRecord.getId());
            }
            if(orderInfo.getScence()==3){
                //支付
                if(orderInfo.getPayment()==1){
                    //余额支付 订单状态改为1 调接口!！！！！！！！
                    //改两个字段 订单状态 支付方式
                    Result result = coc.changeOrderState(orderInfo.getScenceId(),1,PaymentRelated.BALANCE_NAME.getDesc(),DateUtil.timeFormatString(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss"));
                    System.out.println(result);
                    if(result.getCode()!=200) throw new MyException(FundExceptionCode.DAO_ERROR.getCode(),FundExceptionCode.DAO_ERROR.getMsg());
                }
            }
            if(orderInfo.getScence()==4){
                //退款
                //订单改为10 调接口 (输入状态接口)!!!!!!!
                Result result = coc.changeOrderState(orderInfo.getScenceId(),10,orderInfo.getPayment(),DateUtil.timeFormatString(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss"));
                System.out.println(result);
                //添加退款表信息
                ReturnCash returnCash = new ReturnCash();
                returnCash.setCashAmount(orderInfo.getCashAmount());
                returnCash.setCreateTime(LocalDateTime.now());
                returnCash.setCorderId(orderInfo.getScenceId());
                returnCash.setCorderNum(orderInfo.getOut_trade_no());
                returnCash.setPayeeId(orderInfo.getPayeeId());
                returnCash.setPayerId(orderInfo.getPayerId());
                returnCash.setReason("正常退款");
                returnCash.setState(1);
                returnCash.setScence(SchemaCode.REFUND_CODE.getNum());
                returnCash.setPayment(orderInfo.getPayment());
                if(!rc.save(returnCash)) throw new MyException(FundExceptionCode.DAO_ERROR.getCode(),FundExceptionCode.DAO_ERROR.getMsg());
                record.setScenceId(returnCash.getId());
            }
            if(!payRecordService.addInfo(record)) throw new MyException(FundExceptionCode.DAO_ERROR.getCode(),FundExceptionCode.DAO_ERROR.getMsg());
            //防止重消
            srt.opsForValue().set("repeat:defend:info:"+orderInfo.getOut_trade_no(),"done");
    }
}
